package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.ApArticleVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private CacheService cacheService;

    /**
     * 计算热点文章
     */
    @Override
    public void computeHotArticle() {
        //获取近5天的文章数据
        Date date = DateTime.now().minusDays(5).toDate();   //5天前的时刻
        List<ApArticle> articleList = apArticleMapper.findFiveDaysTime(date);

        //计算所有文章的分数权重
        List<ApArticleVo> apArticleVos = articleList.stream().map(article ->{
            ApArticleVo apArticleVo = new ApArticleVo();
            BeanUtils.copyProperties(article,apArticleVo);
            int score = computeScores(article);
            apArticleVo.setScore(score);
            return apArticleVo;
        }).collect(Collectors.toList());

        //获取所有频道
        List<Integer> channels = articleList.stream().map(ApArticle::getChannelId).distinct().collect(Collectors.toList());
        //根据每个频道对文章进行降序排序,获取前30条数据
        for (Integer channel : channels) {
            List<ApArticleVo> apArticleVoLists = apArticleVos.stream().filter(apArticleVo -> apArticleVo.getChannelId().equals(channel)).collect(Collectors.toList());
            List<ApArticleVo> hotArticle = apArticleVoLists.stream().sorted(((o1, o2) -> o2.getScore() - o1.getScore())).limit(30).collect(Collectors.toList());
            //redis缓存
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + channel, JSON.toJSONString(hotArticle));
        }

        //”推荐“频道的热点文章
        List<ApArticleVo> hotArticle = apArticleVos.stream().sorted(((o1, o2) -> o2.getScore() - o1.getScore())).limit(30).collect(Collectors.toList());
        //redis缓存
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG, JSON.toJSONString(hotArticle));

    }

    /**
     * 计算文章分数权重
     * */
    private int computeScores(ApArticle article) {
        Integer score = 0;

        //参数校验
        if(article == null)
            return score;

        //计算分数
        if(article.getLikes() != null){
            score += article.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if(article.getViews() != null){
            score += article.getViews();
        }
        if(article.getComment() != null){
            score += article.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if(article.getCollection() != null){
            score += article.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }

        return score;

    }


}
